home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12435 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Settle a bet please
  5. Date: Sat, 30 Mar 96 21:45:49 GMT
  6. Organization: none
  7. Message-ID: <828222349snz@genesis.demon.co.uk>
  8. References: <4jfopb$o9n@news1.sympatico.ca> <KASPER.96Mar29073654@acm.org> <KASPER.96Mar30085147@acm.org>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <KASPER.96Mar30085147@acm.org> kasperowski@acm.org  writes:
  15.  
  16. > | But
  17. > | 
  18. > |      char name[7] = "My name";
  19. > | 
  20. > | reserves 7 chars, and initializes them to contain
  21. > | 
  22. > |      {'M', 'y', ' ', 'n', 'a', 'm', 'e'}.
  23. > | 
  24. > | This is perfectly legal C, but the resulting array of characters is
  25. >
  26. >In private e-mail, someone said the same thing to me, and I replied,
  27. >"sure, I guess you're right."  I was wrong -- this is illegal.
  28.  
  29. It is perfectly legal.
  30.  
  31. >In ANSI C, 
  32. >
  33. > char name[7] = "My name";
  34. >
  35. >which is the same as
  36. >
  37. > char name[7] = {'M', 'y', ' ', 'n', 'a', 'm', 'e', '\0'};
  38.  
  39. No, it is as was originally stated.
  40.  
  41. >is *not* legal.  Section A8.7 on p. 219 of Appendix A of my copy of
  42. >K&R's _The C Programming Language_, 2nd ed., says:
  43. >
  44. > If the array has fixed size, the number of initializers may not
  45. > exceed the number of members of the array . . .
  46.  
  47. True.
  48.  
  49. >The line specifies an array of fixed size 7 chars, and then
  50. >initializes it with 8 chars.  This is illegal.
  51.  
  52. Not true. This is a special case defined by the language. In that case
  53. only the 7 explicit characters in the string literal are used to initialise
  54. the array. ISO 6.5.7:
  55.  
  56. "An array of character type may be initialized by a character string literal,
  57.  optionally enclosed in braces. Successive characters of the character
  58.  string literal (including the terminating null character if there is room
  59.  or if the array is of unknown size) initialize the elements of the array"
  60.  
  61. The critical part here is ``if there is room''. In this case there isn't
  62. so the null character isn't used as an initializer.
  63.  
  64.  
  65. -- 
  66. -----------------------------------------
  67. Lawrence Kirby | fred@genesis.demon.co.uk
  68. Wilts, England | 70734.126@compuserve.com
  69. -----------------------------------------
  70.